using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Text;
using mEdit.Controls;
using System.Collections;

namespace mEdit
{
    public partial class Options : Form
    {

        public Options()
        {
            InitializeComponent();

            // set colors
            SetColorButtonColors();
            this.scintillaTextBox.EditorFont = Properties.Settings.Default.EditorFont;
            this.cbxAutoSave.Checked = Properties.Settings.Default.Backup;
            this.nudAutoSaveTime.Value = Properties.Settings.Default.AutoSaveTime;
            this.nudAutoSaveTime.Enabled = Properties.Settings.Default.Backup;
        }


        private string _selectedEncodingName = string.Empty;
        public string EncodingName
        {
            get { return _selectedEncodingName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                    return;
                _selectedEncodingName = value;
            }
        }

        private ScintillaNet.CharacterSet _charSet = ScintillaNet.CharacterSet.Default;
        public ScintillaNet.CharacterSet CharSet
        {
            get { return _charSet; }
            set { _charSet = value; }
        }

        public Font EditorFont
        {
            get { return this.scintillaTextBox.EditorFont; }
            protected set
            {
                if (value != null)
                {
                    this.scintillaTextBox.EditorFont = value;
                }
            }
        }

        public Color EditorForeColor
        {
            get { return this.scintillaTextBox.EditorForeColor; }
            protected set
            {
                if (value != Color.Empty)
                {
                    this.scintillaTextBox.EditorForeColor = value;
                    this.cbtnEditorFore.Color = value;
                }
            }
        }

        public Color EditorBackColor
        {
            get { return this.scintillaTextBox.EditorBackColor; }
            protected set
            {
                if (value != Color.Empty)
                {
                    this.scintillaTextBox.EditorBackColor = value;
                    this.cbtnEditorBack.Color = value;
                }
            }
        }

        public Color LineForeColor
        {
            get { return this.scintillaTextBox.LinesForeColor; }
            protected set
            {
                if (value != Color.Empty)
                {
                    this.scintillaTextBox.LinesForeColor = value;
                    this.cbtnLineFore.Color = value;
                }
            }
        }

        public Color LineBackColor
        {
            get { return this.scintillaTextBox.LinesBackColor; }
            protected set
            {
                if (value != Color.Empty)
                {
                    this.scintillaTextBox.LinesBackColor = value;
                    this.cbtnLineBack.Color = value;
                }
            }
        }


        private Color GetColor()
        {
            Color clr = Color.Empty;
            using (ColorDialog clrdlg = new ColorDialog())
            {
                if (clrdlg.ShowDialog(this) == DialogResult.OK)
                {
                    clr = clrdlg.Color; 
                }
            }
            return clr;
        }

        private void cbtnColor_Click(object sender, EventArgs e)
        {
            Color clr = GetColor();

            if (clr == Color.Empty)
                return;

            if (! (sender is ColorButton))
                return;
            switch( ((ColorButton)sender).Name )
            {
                case "cbtnEditorFore":
                    this.EditorForeColor = clr;
                    break;
                case "cbtnEditorBack":
                    this.EditorBackColor = clr;
                    break;
                case "cbtnLineBack":
                    this.LineBackColor = clr;
                    break;
                case "cbtnLineFore":
                    this.LineForeColor = clr;
                    break;
            }
        }

        private void SetColorButtonColors()
        {
            this.EditorBackColor = Properties.Settings.Default.EditorBackColor;
            this.EditorForeColor = Properties.Settings.Default.EditorForeColor;
            this.LineBackColor = Properties.Settings.Default.LineBackColor;
            this.LineForeColor = Properties.Settings.Default.LineForeColor;
        }

        private void btnSetDefaultColors_Click(object sender, EventArgs e)
        {
            //this.LineBackColor = Color.FromArgb(223, 234, 244);
            this.LineBackColor = Color.FromArgb(143, 181, 218);
            this.LineForeColor = Color.White;
            this.EditorBackColor = Color.White;
            this.EditorForeColor = Color.Black;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (FontDialog fdlg = new FontDialog())
            {
                fdlg.Font = this.scintillaTextBox.EditorFont;
                if (fdlg.ShowDialog(this) == DialogResult.OK)
                {
                    this.EditorFont = fdlg.Font;
                }
            }
        }

        private void cbxAutoSave_CheckedChanged(object sender, EventArgs e)
        {
            if (!( sender is CheckBox) )
                return;
            this.nudAutoSaveTime.Enabled = ((CheckBox)sender).Checked;
        }

        private void Options_Load(object sender, EventArgs e)
        {
            // fill encoding combobox
            cbxEncodings.SelectedIndexChanged -= new EventHandler(cbxEncodings_SelectedIndexChanged);
            cbxEncodings.DataSource = System.Text.Encoding.GetEncodings();
            cbxEncodings.DisplayMember = "DisplayName";
            cbxEncodings.DropDownStyle = ComboBoxStyle.DropDownList;
            cbxEncodings.ValueMember = "Name";
            // set selected encoding as the one showed by combobox
            cbxEncodings.SelectedIndexChanged +=new EventHandler(cbxEncodings_SelectedIndexChanged);
            cbxEncodings.SelectedValue = _selectedEncodingName;
        }


        private void cbxEncodings_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxEncodings.SelectedItem == null)
                return;
            _selectedEncodingName = cbxEncodings.SelectedValue as string;
        }

    }

}
